package com.seari.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     *
     * @param cal
     * @returnss
     */
    public static String formatToString(Calendar cal, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String str = sdf.format(cal.getTime());
        return str;
    }

    public static Date getDate() {
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }

    /**
     * 日期转换为Long类型
     * @param str
     * @return
     */
    public static Long getDateByLong(String str) {
        if (str != null && str.length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(str);
                return date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;

    }

    /**
     * 日期转换为Long类型
     * @param str
     * @return
     */
    public static Long getStartDate(Date str) {
        if (str != null) {
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(sdf.format(str));
//				long addTime = 30*1000;
//				return date.getTime()-addTime;
                return date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;

    }

    /**
     * 日期转换为Long类型
     * @param str
     * @return
     */
    public static Long getEndDate(Date str) {
        if (str != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = sdf.parse(sdf.format(str));
                return date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;

    }

    /**
     * 获取当前String类型时间
     * @return
     */
    public static String getNowDate() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.format(localDateTime);
    }

    public static String getNowDateStr() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");
        return dtf.format(localDateTime);
    }



    public static void main(String[] args) {
        Long aa = getStartDate(new Date());
        Long bb = getEndDate(new Date());
        System.out.println(aa);
        System.out.println(bb);
        System.out.println(aa - bb);
        String t = getNowDateStr();
        System.out.println(t);

    }

}
